package com.tueye.service.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.tueye.base.util.Page;
import com.tueye.entity.Adinfo;
import com.tueye.entity.Adsite;
import com.tueye.entity.SysDic;
import com.tueye.entity.SysDicValue;
import com.tueye.entity.Topics;
import com.tueye.service.exception.TuEyeServiceException;
import com.tueye.utils.ByteSubString;
import com.tueye.utils.FileHandle;
import com.tueye.utils.TuEyeConstant;
/**
 * 生成html页面总控制器
 * 
 * @author hanxiaowei
 *
 */
@Scope("prototype")
@Component("builderTemplateServie")
public class BuilderTemplateServieImpl extends AbstractBuilderHtml{

	private static String file_spe = File.separator;
	
	/*public BuilderTemplateServieImpl(){
		super();
	}*/
	@Override
	public void buildHtml() throws TuEyeServiceException {
		
		try {
			List<SysDic> sysDicList = sysDicDao.getSysDicByAll();
			if(!sysDicList.isEmpty()&&sysDicList.size()>0){
				String templateName = "";
				for (SysDic sysDic:sysDicList) {
					templateName = sysDic.getTemplatePlate();
					List<SysDicValue> sysDicValueList = sysDicDao.getSysDicValueByHql("from SysDicValue where templateId='"+sysDic.getId()+"'");
					Map<String,Object> dataModel=new HashMap<String,Object>();
					if(!sysDicValueList.isEmpty()&&sysDicValueList.size()>0){
						for (SysDicValue sysDicValue:sysDicValueList) {
							if(sysDicValue.getModulePlate().equalsIgnoreCase(TuEyeConstant.REPLACE_TOP30)){
								dataModel.put(sysDicValue.getModulePlate(),getTopHtmlContent(sysDicValue.getId(),sysDicValue.getModuleHtml(), 1, 30));
							}else if(sysDicValue.getModulePlate().equalsIgnoreCase(TuEyeConstant.REPLACE_TOP20)){
								dataModel.put(sysDicValue.getModulePlate(),getTopHtmlContent(sysDicValue.getId(),sysDicValue.getModuleHtml(), 1, 20));
							}else{
								dataModel.put(sysDicValue.getModulePlate(),getHtmlContent(sysDicValue.getId(),sysDicValue.getModuleHtml()));
							}
						}
					}
					String htmlFileName = TuEyeConstant.PROJECTPATH;
					if(StringUtils.isNotEmpty(sysDic.getTemplateEnlishName())&&!sysDic.getTemplateEnlishName().equalsIgnoreCase("index")){
						htmlFileName += sysDic.getTemplateEnlishName();
					}
					FileHandle fileHandle = new FileHandle();
					fileHandle.cerateDirectory(htmlFileName);
					/*String htmlName = sysDic.getTemplatePlate();
					htmlName = StringUtils.isEmpty(htmlName)?"index.html":htmlName.substring(0, htmlName.lastIndexOf("."))+".html";*/
					htmlFileName  = htmlFileName + file_spe + "index.html";
					String htmlString = iTemplate.processTemplateIntoString(templateName, dataModel);
					FileOutputStream out = new FileOutputStream(new File(htmlFileName));
					out.write(htmlString.getBytes());
					out.close();
				}
			}
		} catch (Exception e) {
			throw new TuEyeServiceException("生成Html失败.",e);
		}
	}
	
	@Override
	public String getHtmlContent(String moduleId ,final String moduleHtml){
		
		StringBuffer dataHtml = new StringBuffer();
		String hql = "from com.tueye.entity.Adsite where moduleId='"+moduleId+"'";
		List<Adsite> adsiteList = adsiteDao.getAdsiteByHql(hql);
		for (Adsite adsite:adsiteList) {
			List<Adinfo> adinfoList = adsite.getAdinfos();
			if(adinfoList.isEmpty()||adinfoList.size()<0){
				return "";
			}
			String newpath = TuEyeConstant.PROJECT_TOPIC_PATH +File.separatorChar+"aduploadimg"+File.separatorChar ;
			for (Adinfo adinfo:adinfoList) {
				String newModuleHtml = moduleHtml;
				newModuleHtml = moduleHtml.replaceAll(TuEyeConstant.REPLACE_TOPIC_TITLE, adinfo.getAdinfo_title_length()!=0?
						ByteSubString.bSubString(adinfo.getAdinfo_title(), adinfo.getAdinfo_title_length()):adinfo.getAdinfo_title() );
				newModuleHtml = newModuleHtml.replaceAll(TuEyeConstant.REPLACE_TOPIC_DESCRIPTION, adinfo.getAdinfo_description_length()!=0?
						ByteSubString.bSubString(adinfo.getAdinfo_description(), adinfo.getAdinfo_description_length()):adinfo.getAdinfo_description());
				newModuleHtml = newModuleHtml.replaceAll(
						adinfo.getPicture_site_type().equalsIgnoreCase("1")
							?newpath+adinfo.getAdpicture_site():TuEyeConstant.REPLACE_TOPIC_IMGURL, adinfo.getAdpicture_site());

				dataHtml.append(newModuleHtml);
			}
		}
		return dataHtml.toString();
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getTopHtmlContent(String moduleId, final String moduleHtml, int pageNo, int pageSize) {
		
		String hql = " FROM com.tueye.entity.Topics t order   by topic_clickCount desc ";//LIMIT pageNo,pageSize 
		Page page = topicDao.pagedQueryByHQL(hql, pageNo, pageSize, null);
		StringBuffer topDataHtml = new StringBuffer();
		List<Topics> topicList = (List<Topics>) page.getResult();
		if(topicList.isEmpty()&&topicList.size()<0){
			return "";
		}
		for (Topics topics:topicList) {
			String newModuleHtml = moduleHtml;
			newModuleHtml = moduleHtml.replaceAll(TuEyeConstant.REPLACE_TOPIC_TITLE,topics.getTopic_title());
			topDataHtml.append(newModuleHtml);
		}
		return topDataHtml.toString();
	}
	
}
